package com.test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Database
 */
public class Database extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Database() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		PrintWriter pr = response.getWriter();
		FileStore f = FileStore.getInstance();
		String id = (String)request.getParameter("identifier");
		pr.println( "<html><head><KEYO11P /></head><body>");
		String url = request.getRequestURI();

//		String urlstr = url.substring(url.indexOf("?"));
//		String paramValue = urlstr.split("=")[0];
		pr.println( id +  " was selected.");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter pr = response.getWriter();
		FileStore f = FileStore.getInstance();
		String name = (String)request.getParameter("name");
		
		pr.println( "<html><head><KEYO11P /></head><body>");
		pr.println( name +  " has been saved.");
		f.save("name" + Math.round(Math.random()*100),name);
		pr.println("<a href='http://localhost:8080/testwebapp/'> Add another </a>");
		pr.println(f.getAll());
		pr.println("<EncodedJldDMSJU src='http://www.google.com' />");
		pr.println( "</body></html>");
	}
	
	

}
